using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.Design;


namespace TVDesigner
{
	/// <summary>
	/// Summary description for UserControl1.
	/// </summary>
	///
    public class ColorComboBox : System.Windows.Forms.ListBox
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
        private Color _Color;


		public ColorComboBox()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
			// TODO: Add any initialization after the InitComponent call

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// ColorComboBox
			// 
			this.AccessibleRole = System.Windows.Forms.AccessibleRole.ComboBox;
			this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
			//this.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			//this.Size = new System.Drawing.Size(120, 24);
            this.Dock = DockStyle.Fill;
			this.EnabledChanged += new System.EventHandler(this.ColorComboBox_EnabledChanged);
			this.SelectedIndexChanged += new System.EventHandler(this.ColorComboBox_SelectedIndexChanged);
			this.MouseEnter += new System.EventHandler(this.ColorComboBox_MouseEnter);
			this.MouseLeave += new System.EventHandler(this.ColorComboBox_MouseLeave);
			this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ColorComboBox_DrawItem);

		}
		#endregion

		[Browsable(true)]
		[DefaultValue(1)]
		[Category("Property")]
		public Color Color
		{
			get  
			{ 
				return _Color;
			}
			set 
			{				
				_Color = value;
				this.Refresh();
			}
		}		

		private void DrawCombo(object sender, System.Windows.Forms.DrawItemEventArgs e)
		{
            Graphics g = e.Graphics;
            Rectangle rd = e.Bounds;

            if (e.Index >= 0)
            {
                if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.White), rd);
                    e.DrawFocusRectangle();
                }
                else
                {
                    e.Graphics.FillRectangle(new SolidBrush(SystemColors.Highlight), rd);
                    e.DrawFocusRectangle();
                }
            }

            rd.Width = 20;
            rd.Height = this.ItemHeight - 5;
            rd.X = 4;
            rd.Y += 1;

            if (e.Index >= 0 && e.Index < this.Items.Count)
            {
                string itemName = this.Items[e.Index].ToString();

                g.FillRectangle(new SolidBrush(_Color), rd);
                g.DrawRectangle(new Pen(Color.Black), rd);
                if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
                    g.DrawString(itemName, this.Font, new SolidBrush(Color.Black), rd.Width + 5, rd.Top - 1);
                else
                    g.DrawString(itemName, this.Font, new SolidBrush(SystemColors.HighlightText), rd.Width + 5, rd.Top - 1);
            }
		}

		private void ColorComboBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
		{
            if (this.Items.Count < _Items.Count)
            {
                if (this.Height < 21) this.Height = 21;
                if (this.ItemHeight < 15) this.ItemHeight = 15;
                for (int j = this.Items.Count; j < _Items.Count; j++)
                	this.Items.Add(_Items[j]);

                //int i = 0;
                //Console.WriteLine(this.Items.Count);
                //for (i = 0; i < this.Items.Count - 1; i++)
                //{
                //    if (_Items[i] == this._SelectedColumnName)
                //    {
                //        this.SelectedIndex = i;
                //        break;
                //    }
                //}
                this.SelectedIndex = _SelectedIndex;
            }
			DrawCombo(sender, e);			
		}

        private int _SelectedIndex = -1;

		private void ColorComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
            _SelectedIndex = this.SelectedIndex;
		}

		private void ColorComboBox_MouseEnter(object sender, System.EventArgs e)
		{
			Graphics gr = this.CreateGraphics();
			gr.FillRectangle(new SolidBrush(Color.FromArgb(90,255,255,255)),this.Width-this.Height+2,3,this.Height-5,this.Height-6);	
		}

		private void ColorComboBox_MouseLeave(object sender, System.EventArgs e)
		{
			this.Invalidate(new Rectangle(this.Width-this.Height+2,3,this.Height-5,this.Height-6));
			this.Update();		
		}

		private void ColorComboBox_EnabledChanged(object sender, System.EventArgs e)
		{
			if(this.Enabled)
				this.BackColor = Color.White;
			else
                this.BackColor = SystemColors.ControlDark;
			
		}

        private System.Collections.Generic.List<string> _Items = new System.Collections.Generic.List<string>();

        public void Add(string item)
        {
            this.Items.Add(item);
            _Items.Add(item);
        }
    }
}
